<!DOCTYPE html>
<html lang="en">
<link rel="stylesheet" href="/webjars/bootstrap/3.3.7-1/css/bootstrap.min.css"/>
<script src="/webjars/jquery/3.1.1/jquery.min.js"></script>
<script src="/webjars/bootstrap/3.3.7-1/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="/js/jquery-easyui-1.8.6/themes/bootstrap/easyui.css"/>
<link rel="stylesheet" type="text/css" href="/js/jquery-easyui-1.8.6/themes/icon.css"/>
<link rel="stylesheet" type="text/css" href="/js/jquery-easyui-1.8.6/themes/color.css"/>
<script type="text/javascript" src="/js/jquery-easyui-1.8.6/jquery.min.js"></script>
<script type="text/javascript" src="/js/jquery-easyui-1.8.6/jquery.easyui.min.js"></script>
<script type="text/javascript" src="/js/jquery-easyui-1.8.6/locale/easyui-lang-zh_CN.js"></script>
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div id="di" style="height: 400px;overflow-y: scroll;padding-top: 0px;border: black">
</div>
<div>
    <input type="button" value="提交" onClick="batchCommit()">
    <input type="button" value="生成表格" onClick="createTable()">
    <input type="button" value="跳转表格" onClick="createWin()">
</div>

<div id="infoWindow" iconCls="icon-save" class="easyui-window" style="height: 400px;width:600px;padding: 5px;">

</div>
<div id="saveOpen">
    <div  style="text-align: center;padding: 5px;">
        <a id="save-window" class="easyui-linkbutton" iconCls="icon-ok" >保存</a>
        <a class="easyui-linkbutton" iconCls="icon-cancel" onclick="closeWin()">取消</a>
    </div>
</div>


</body>
<script>
    var errList;
    var menuWin;
    $(function () {
        $("#saveOpen").hide();
        menuWin = $("#infoWindow").window({
            width: '650',
            height: '450',
            modal: true,
            collapsible: true,
            shadow: false,
            inline: false,
            closed: true,
            minimizable: false,
            maximizable: false,
            resizable: true
        })
        $("#infoWindow").window({
            onBeforeClose:function () {
                $("#saveOpen").hide();
            }
        })

    })
    
    
    function createWin() {
        menuWin.window({
            title: '编辑',
            href: ''
        })
    }

    function createTable() {

        $.ajax({  // ajax登陆请求
            url: "/batch/list",
            type: "POST",
            dataType: "json",
            async: false,
            success: function (data) {
                if (data.result) {
                    errList = data.diffList;
                }
            }
        });

        menuWin.window({title: '批量编辑'});
        menuWin.window('open').window('refresh');

        var div = $("#infoWindow");
        $("#infoWindow").html('');

        //每次循环生成一个表格
        for (var errnum = 0; errnum < errList.length; errnum++) {

            /*1.定义一个表格*/
            var title = "<table class='messageTab' id='messageTab" + errnum + "' cellpadding='3' cellspacing='0'  style='border:1px #00F solid; margin-bottom: 5px;' > </table>";
            /*2.把表格增加到div中*/
            div.append(title);
            /*3.对表格进行操作*/
            $("#messageTab" + errnum).html('');//清空表格中的行和列
            var tbodyObj = $("#messageTab" + errnum);  //获取表格，给表格添加行和列
            tbodyObj.append(
                "<input id='mappId" + errnum + "' hidden value='" + errList[errnum].mappId + "' disabled/>" +
                "<tr><td>" + "商户号" + "</td><td><input id='mchntNo" + errnum + "'  value='" + errList[errnum].mchntNo + "' disabled /></td>" +
                "<td>" + "所属机构" + "</td><td><input id='orgId" + errnum + "'  value='" + errList[errnum].orgId + "' disabled /></td></tr>" +
                "<tr><td>" + "结算调整金额" + "</td><td><input id='settleAmtAdjust" + errnum + "' oninput='changeSettleAmt(" + errnum + "," + errList[errnum].settleAmt + ")' type='number' /></td>" +
                "<td>" + "调整后额度(试算)" + "</td><td> <input id='settleAmt" + errnum + "'  value='" + errList[errnum].settleAmt + "'  disabled /></td></tr>" +
                "<tr><td>" + "悦融益调整金额" + "</td><td><input id='AmtAdjust" + errnum + "' oninput='changeAmt(" + errnum + "," + errList[errnum].Amt + ")' type='number' /></td>" +
                "<td>" + "调整后额度(试算)" + "</td><td> <input id='Amt" + errnum + "' value='" + errList[errnum].Amt + "'   disabled /></td></tr>" +
                "<tr><td>" + "调整原因" + "</td><td colspan='3'> <input id='remark" + errnum + "' value='" + errList[errnum].remark + "' /></td></tr>" +
                "<tr><td>" + "操作人员" + "</td><td> <select id='operator" + errnum + "'><option value='111'>张三</option> <option value='222'>李四</option></select> </td></tr>"
            );
        }
       // var saveDiv = "<div style='text-align: center;padding: 5px;'><a id='save-window' class='easyui-linkbutton' iconCls='icon-ok'>保存</a><a class='easyui-linkbutton' iconCls='icon-cancel' onclick='closeWin()'>取消</a> </div>"

        var openWindow = $("#saveOpen").html();
        div.append(openWindow);

        $("#saveOpen").show();

    }

    function closeWin() {
        $("#infoWindow").window('close');
        $("#saveOpen").hide();
    }



    function changeSettleAmt(errnum, m) {
        var settleAmtAdjust = $("#settleAmtAdjust" + errnum).val();
        if (parseInt(settleAmtAdjust) >= 0) {
            settleAmtAdjust = "+" + settleAmtAdjust;
        }
        var settleAmt = m + '' + settleAmtAdjust;
        $("#settleAmt" + errnum).val(settleAmt);
    }

    function changeAmt(errnum, m) {
        var AmtAdjust = $("#AmtAdjust" + errnum).val();
        if (parseInt(AmtAdjust) >= 0) {
            AmtAdjust = "+" + AmtAdjust;
        }
        var Amt = m + '' + AmtAdjust;
        $("#Amt" + errnum).val(Amt);
    }

    function batchCommit() {
        var list = new Array();
        for (var errnum = 0; errnum < errList.length; errnum++) {
            var obj = new Object();
            obj.mappId = errList[errnum].mappId;
            obj.orgId = errList[errnum].orgId;
            obj.mchntNo = errList[errnum].mchntNo;
            var settleAmtAdjust = $("#settleAmtAdjust" + errnum).val();
            if (settleAmtAdjust === null || settleAmtAdjust === "") {
                settleAmtAdjust = "0";
            }
            obj.settleAmtAdjust = settleAmtAdjust;
            obj.settleAmt = parseInt(errList[errnum].settleAmt) + parseInt(settleAmtAdjust);

            var AmtAdjust = $("#AmtAdjust" + errnum).val();
            if (AmtAdjust === null || AmtAdjust === "") {
                AmtAdjust = "0";
            }
            obj.AmtAdjust = AmtAdjust;
            obj.Amt = parseInt(errList[errnum].Amt) + parseInt(AmtAdjust);


            obj.remark = $("#remark" + errnum).val();
            obj.operator = $("#operator" + errnum).val();
            list.push(obj);
        }

        $.ajax({  // ajax登陆请求
            url: "/batch/update",
            type: "POST",
            data: {params: JSON.stringify(list)},
            async: false,
            success: function (data) {
                if (data.result) {
                    alert("success");
                } else {
                    alert(data.msg);
                }
            }
        });


    }


</script>
<style>
    .messageTab {
        width: 99%;
        margin-top: 10px;
        margin-left: 5px;
        font-size: 13px;
        font-weight: normal;
        border: 2px;
    }

</style>
</html>